package com.laicunba.jms;

import javax.jms.JMSException;
import javax.jms.Message;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.laicunba.pojo.MessageDO;
import com.laicunba.service.MessageService;
import com.laicunba.util.AssertUtil;

public abstract class JmsQueueListenerTemplate implements JmsQueueListener {

	@Autowired
	private MessageService messageService;

	protected MessageDO getMessageDO(String jmsMessageID) {
		return messageService.get(MessageDO.class, "mq_" + jmsMessageID);
	}
	protected boolean updateMessageDO(MessageDO msgDO) {
		return messageService.updateSkipCheckOwner(msgDO);
	}
	@Override
	public boolean _receive(Message msg) {
		if (!receive(msg)) {
			return false;
		}

		try {
			AssertUtil.assertTrue(messageService.incrUsedCount("mq_" + msg.getJMSMessageID()), "UPDATE_QUEUE_USED_COUNT_FAIL");
		} catch (JMSException e) {
			e.printStackTrace();
		}

		return true;
	}

}
